package com.seagox.chat.util;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class TreeUtils {

	public static JSONArray listToTreeByRule(JSONArray arr, String key, String rule) {
        String[] ruleArray = rule.split("");
        List<Integer> ruleList = new ArrayList<>();
        JSONObject ruleMap = new JSONObject();
        for (int i = 0; i < ruleArray.length; i++) {
            if (i == 0) {
                ruleList.add(Integer.valueOf(ruleArray[i]));
                ruleMap.put(ruleArray[i], i);
            } else {
                ruleList.add(ruleList.get(i - 1) + Integer.valueOf(ruleArray[i]));
                ruleMap.put(String.valueOf(ruleList.get(i - 1) + Integer.valueOf(ruleArray[i])), i);
            }
        }

        JSONArray r = new JSONArray();
        JSONObject hash = new JSONObject();
        // 将数组转为Object的形式，key为数组中的id
        for (int i = 0; i < arr.size(); i++) {
            JSONObject json = (JSONObject) arr.get(i);
            hash.put(json.getString(key), json);
        }
        // 遍历结果集
        for (int j = 0; j < arr.size(); j++) {
            // 单条记录
            JSONObject aVal = (JSONObject) arr.get(j);
            // 在hash中取出key为单条记录中pid的值
            String curCode = aVal.get(key).toString();

            int index = ruleMap.getIntValue(String.valueOf(curCode.length()));

            String curKey = null;
            if (index != 0) {
                curKey = curCode.substring(0, ruleList.get(index - 1));
            }
            JSONObject hashVP = (JSONObject) hash.get(curKey);
            // 如果记录的pid存在，则说明它有父节点，将她添加到孩子节点的集合中
            if (hashVP != null) {
                // 检查是否有child属性
                if (hashVP.get("children") != null) {
                    JSONArray ch = (JSONArray) hashVP.get("children");
                    ch.add(aVal);
                    hashVP.put("children", ch);
                } else {
                    JSONArray ch = new JSONArray();
                    ch.add(aVal);
                    hashVP.put("children", ch);
                }
            } else {
                r.add(aVal);
            }
        }
        return r;
    }
	
	public static JSONArray listToTreeByRule(JSONArray arr, String key, String rule, JSONObject userObject) {
        String[] ruleArray = rule.split("");
        List<Integer> ruleList = new ArrayList<>();
        JSONObject ruleMap = new JSONObject();
        for (int i = 0; i < ruleArray.length; i++) {
            if (i == 0) {
                ruleList.add(Integer.valueOf(ruleArray[i]));
                ruleMap.put(ruleArray[i], i);
            } else {
                ruleList.add(ruleList.get(i - 1) + Integer.valueOf(ruleArray[i]));
                ruleMap.put(String.valueOf(ruleList.get(i - 1) + Integer.valueOf(ruleArray[i])), i);
            }
        }

        JSONArray r = new JSONArray();
        JSONObject hash = new JSONObject();
        // 将数组转为Object的形式，key为数组中的id
        for (int i = 0; i < arr.size(); i++) {
            JSONObject json = (JSONObject) arr.get(i);
            hash.put(json.getString(key), json);
        }
        // 遍历结果集
        for (int j = 0; j < arr.size(); j++) {
            // 单条记录
            JSONObject aVal = (JSONObject) arr.get(j);
            // 在hash中取出key为单条记录中pid的值
            String curCode = aVal.get(key).toString();

            int index = ruleMap.getIntValue(String.valueOf(curCode.length()));

            String curKey = null;
            if (index != 0) {
                curKey = curCode.substring(0, ruleList.get(index - 1));
            }
            JSONObject hashVP = (JSONObject) hash.get(curKey);
            if (hashVP != null) {
                // 检查是否有children属性
                if (hashVP.get("children") != null) {
                    JSONArray ch = (JSONArray) hashVP.get("children");
                    if(userObject.containsKey(aVal.getString("code"))) {
                    	aVal.put("children", userObject.getJSONArray(aVal.getString("code")));
                	}
                    ch.add(aVal);
                    hashVP.put("children", ch);
                } else {
                    JSONArray ch = new JSONArray();
                    if(userObject.containsKey(aVal.getString("code"))) {
                    	aVal.put("children", userObject.getJSONArray(aVal.getString("code")));
                	}
                    ch.add(aVal);
                    hashVP.put("children", ch);
                }
            } else {
            	if(userObject.containsKey(aVal.getString("code"))) {
            		aVal.put("children", userObject.getJSONArray(aVal.getString("code")));
            	}
                r.add(aVal);
            }
        }
        return r;
    }

}